package com.sproutcat.sample;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class InterruptExample {

    private static class MyThread1 implements Runnable {
        @Override
        public void run() {
            try {
                Thread.sleep(2000);
                System.out.println("Thread run");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private static class MyThread2 extends Thread {
        @Override
        public void run() {
            while (!interrupted()) {
                // ..
            }
            System.out.println("Thread end");
        }
    }

    public static void main(String[] args) {
//        Thread thread1 = new Thread(new MyThread1());
//        thread1.start();
//        thread1.interrupt();
//        System.out.println("Main run");

//        Thread thread2 = new MyThread2();
//        thread2.start();
//        thread2.interrupt();

        ExecutorService executorService = Executors.newCachedThreadPool();
        Future<?> future = executorService.submit(() -> {
            try {
                Thread.sleep(2000);
                System.out.println("Thread run");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        future.cancel(true);
//        executorService.shutdownNow();
        System.out.println("Main run");
    }

}
